package com.dc.controller;

import com.dc.common.ResponseResultVO;
import com.dc.entity.SysJob;
import com.dc.service.ISysJobService;
import com.dc.util.CronUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 调度任务信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/job")
@Api(tags = "调度任务信息操作")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时任务列表
     */
    @ApiOperation(value = "查询定时任务列表", notes = "查询定时任务列表")
    @GetMapping("/list")
    public ResponseResultVO list(SysJob sysJob) {
        List<SysJob> list = jobService.selectJobList(sysJob);
        return ResponseResultVO.successResult(list);
    }

    /**
     * 获取定时任务详细信息
     */
    @ApiOperation(value = "获取定时任务详细信息", notes = "获取定时任务详细信息")
    @GetMapping(value = "/{jobId}")
    public ResponseResultVO getInfo(@PathVariable("jobId") String jobId) {
        return ResponseResultVO.successResult(jobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @ApiOperation(value = "新增定时任务", notes = "新增定时任务")
    @PostMapping
    public ResponseResultVO add(@RequestBody SysJob job) throws SchedulerException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return ResponseResultVO.failResult("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        }
        return ResponseResultVO.insertResult(jobService.insertJob(job));
    }

    /**
     * 修改定时任务
     */
    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    @PutMapping
    public ResponseResultVO edit(@RequestBody SysJob job) throws SchedulerException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return ResponseResultVO.failResult("修改任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        }
        return ResponseResultVO.updateResult(jobService.updateJob(job));
    }

    /**
     * 定时任务状态修改
     */
    @ApiOperation(value = "定时任务状态修改", notes = "定时任务状态修改")
    @PutMapping("/changeStatus")
    public ResponseResultVO changeStatus(@RequestBody SysJob job) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(String.valueOf(job.getJobId()));
        newJob.setStatus(job.getStatus());
        return ResponseResultVO.updateResult(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @ApiOperation(value = "定时任务立即执行一次", notes = "定时任务立即执行一次")
    @PutMapping("/run")
    public ResponseResultVO run(@RequestBody SysJob job) throws SchedulerException {
        jobService.run(job);
        return ResponseResultVO.successResult("成功");
    }

    /**
     * 删除定时任务
     */
    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    @DeleteMapping("/deleteByJobIds")
    public ResponseResultVO remove(@PathVariable String jobIds) throws SchedulerException {
        jobService.deleteJobByIds(jobIds);
        return ResponseResultVO.successResult("成功");
    }
}
